var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var app = getApp();

Page({
    data: {
        rechargeAmount: ''
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        // 页面显示

    },
    bindAmountInput: function (e) {
        this.setData({
            rechargeAmount: e.detail.value
        });
    },
    userRecharge: function (e) {
        if (!app.globalData.hasLogin) {
            wx.navigateTo({
                url: "/pages/auth/login/login"
            });
        }
        let that = this;
        if (that.data.rechargeAmount == '') {
            util.showErrorToast('请输入充值金额');
            return false;
        }
        if (that.data.rechargeAmount == 0) {
            util.showErrorToast('充值金额应大于零');
            return false;
        }
        wx.showLoading({
            title: '提交中...',
            mask: true,
            success: function () {
            }
        });
        util.request(api.RechargeAdd, {
            rechargeAmount: that.data.rechargeAmount
        }, 'POST').then(function (res) {
            wx.hideLoading();
            if (res.errno === 0) {
                wx.showToast({
                    title: '充值成功！',
                    icon: 'success',
                    duration: 2000,
                    complete: function () {
                        that.setData({
                            rechargeAmount: ''
                        });
                    }
                }),
                    wx.setStorage({
                        key: "remainAmount",
                        data: res.data.remainAmount,
                        success: function () {
                            wx.switchTab({
                                url: '/pages/ucenter/index/index'
                            });
                        }
                    });
                // wx.setStorageSync('remainAmount', res.data.remainAmount);
                // wx.switchTab({
                //   url: '/pages/ucenter/index/index'
                // });
            } else {
                util.showErrorToast(res.errmsg);
            }
        });
    },
    onHide: function () {
        // 页面隐藏
    },
    onUnload: function () {
        // 页面关闭
    }
})